package com.cn.controller;

import com.cn.po.*;
import com.cn.vo.ErrorWordVo;
import com.cn.service.WangLuWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/wanglu")
public class WangLuWordController {
    @Autowired
    WangLuWordService wangLuWordService;

    @GetMapping
    public Result getAll(){
        List<WordWL> wordWLList = wangLuWordService.getAll();
        Integer code = wordWLList != null? Code.GET_OK : Code.GET_ERR;
        String msg = wordWLList != null? "" : "数据查询失败，请重试!";
        return new Result(code,wordWLList,msg);
    }

    // 获取章节列表
    @GetMapping("/chapters")
    public Result getChapters() {
        List<String> chapters = wangLuWordService.getDistinctChapters();
        return new Result(Code.GET_OK, chapters, "");
    }

    // 分页查询单词，支持按章节和搜索条件过滤
    @GetMapping("/pagequery")
    public Result getWordsByPage(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "50") int size,
            @RequestParam(value = "query", required = false) String query,
            @RequestParam(value = "chapter", required = false) String chapter
    ) {
        int offset = (page - 1) * size;
        List<WordWL> wordWLList = wangLuWordService.getWordsByPage(query, chapter, offset, size);
        int total = wangLuWordService.getTotalCount(query, chapter);

        Map<String, Object> result = new HashMap<>();
        result.put("data", wordWLList);
        result.put("total", total);

        return new Result(Code.GET_OK, result, "");
    }

    // 获取指定章节的所有单词
    @GetMapping("/test/words")
    public Result getWordsByChapter(@RequestParam("chapter") String chapter){
        List<WordWL> words = wangLuWordService.getWordsByChapter(chapter);
        return new Result(200,words,"");
    }

    @PostMapping("/test/submit")
    public Result submitTest(@RequestBody FrontPojo payload){
        ArrayList<Integer> errorWordIds = new ArrayList<>();
        // 返回前端的结果集
        ArrayList<Map<String,Object>> result = new ArrayList<>();
        // 获取用户的测试记录
        ArrayList<TestWord> userAnswers = payload.getAnswers();
        for (TestWord userAnswer : userAnswers) {
            Integer wordId = userAnswer.getWordId();
            //用户输入的单词
            String answerWord = userAnswer.getUserAnswer();
            //数据库中的单词
            WordWL word = wangLuWordService.getWordById(wordId);
            Map<String,Object> wordResult = new HashMap<>();
            wordResult.put("id",word.getId());
            wordResult.put("word",word.getWord());
            wordResult.put("usPhonetic",word.getUsPhonetic());
            wordResult.put("usVoice",word.getUsVoice());
            wordResult.put("ukPhonetic",word.getUkPhonetic());
            wordResult.put("ukVoice",word.getUkVoice());
            wordResult.put("wangLuMeaning",word.getWangLuMeaning());
            wordResult.put("userAnswer",userAnswer);
            wordResult.put("isCorrect",answerWord.equalsIgnoreCase(word.getWord()));
            //用户回答是错的，记录错误的单词的ID
            if(!answerWord.equalsIgnoreCase(word.getWord())){
                errorWordIds.add(wordId);
            }
            //添加该单词的测试结果
            result.add(wordResult);
        }

        // 更新错误次数
        wangLuWordService.updateErrorCounts(errorWordIds);

        return new Result(200,result,"测试结果");
    }

}
